package com.bdqn.controller;


import com.bdqn.entity.Dish;
import com.bdqn.entity.DishFlavor;
import com.bdqn.param.requset.DishParam;
import com.bdqn.param.response.DishDto;
import com.bdqn.param.response.Page;
import com.bdqn.service.DishFlavorService;
import com.bdqn.service.DishService;
import com.bdqn.util.RespBean;
import com.bdqn.util.ResultEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * <p>
 * 菜品管理 前端控制器
 * </p>
 *
 * @author hunter
 * @since 2022-06-11
 */
@Slf4j
@RestController
@RequestMapping("/dish")
public class DishController {
    @Autowired
    private DishService dishService;
    @Autowired
    private DishFlavorService dishFlavorService;

    @GetMapping("/page")
    public RespBean page(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "10") Integer pageSize, String name) {
        Page<DishDto> dishPage = dishService.pageInfo(page, pageSize, name);
        return dishPage != null ? RespBean.ok("获取分类管理列表成功", dishPage) : RespBean.error("获取分类管理列表失败");
    }

    /**
     * 添加一个菜品 同时添加这个菜品的多个口味
     *
     * @param dishParam
     * @return
     */
    @PostMapping
    public RespBean addDish(@RequestBody DishParam dishParam) {
////         添加菜品信息
//        Dish dish = new Dish();
//        dish.setName(dishParam.getName());
//        dish.setCategoryId(dishParam.getCategoryId());
//        dish.setCode(dishParam.getCode());
//        dish.setPrice(dishParam.getPrice());
//        dish.setImage(dishParam.getImage());
//        dish.setStatus(dishParam.getStatus());
//        dish.setDescription(dishParam.getDescription());
        //这里可以使用BeanUtils.copyProperties()-->将一个对象的属性 赋值给另一个对象
        boolean save = dishService.save(dishParam);
        if (!save) {
            return new RespBean(ResultEnum.FAIL);
        }
        //添加菜品口味
        for (DishFlavor dishFlavor : dishParam.getFlavors()) {
            dishFlavor.setDishId(dishParam.getId());
            System.out.println("展示的dishFlavor数据为" + dishFlavor);
            boolean save1 = dishFlavorService.save(dishFlavor);
            if (!save1) {
                return new RespBean(ResultEnum.FAIL);
            }
        }
        return save ? RespBean.ok("添加菜品成功", save) : RespBean.error("添加菜品失败");
    }

    /**
     * 通过id查询菜品  完成修改前的回显操作
     */
    @GetMapping("{id}")
    public RespBean toUpdate(@PathVariable Long id) {
        DishDto dishDto = dishService.dishDto(id);
        return dishDto != null ? RespBean.ok("通过id查询菜品成功", dishDto) : RespBean.error("通过id查询菜品失败");
    }

    /**
     * 接收前台传来的修改菜品的数据对象 进行对菜品信息及菜品的口味的修改
     */
    @PutMapping
    public RespBean update(@RequestBody DishParam dishParam) {
        boolean flag = dishService.updateById(dishParam);

        log.info("获取的前台传的参数为: {}", dishParam);
        log.info("获取的前台传的dish参数为: {}", dishParam.getId() + dishParam.getImage() + dishParam.getDescription());
        System.out.println("获取的前台传的参数为: {}" + dishParam);
        //对该菜品的口味进行修改操作
        for (DishFlavor flavor : dishParam.getFlavors()) {
            flavor.setDishId(dishParam.getId());
            dishFlavorService.updateById(flavor);
        }
        return flag ? RespBean.ok("修改菜品信息成功", flag) : RespBean.error("修改菜品信息失败");
    }

    /**
     * 通过id进行删除对应的菜单信息对象
     */
    @DeleteMapping
    public RespBean delete(Long[] ids) {
        System.out.println("获取的前台传递的参数为" + Arrays.toString(ids));
        try {
            for (Long id : ids) {

                dishService.removeById(id);
            }
            return RespBean.ok("删除菜品成功", 1);
        } catch (Exception e) {
            return RespBean.error("删除菜品失败");
        }
    }

    /**
     * 修改订单的状态
     */
    @PostMapping("/status/{code}")
    public RespBean updateStatus(@PathVariable Integer code, Long[] ids) {
        try {
            Dish dish = new Dish();
            for (Long id : ids) {
                dish.setStatus(code);
                dish.setId(id);
                dishService.updateById(dish);
            }
            return RespBean.ok("更改状态成功", 1);
        } catch (Exception e) {
            return RespBean.error("更改状态失败");
        }
    }

    /**
     * 通过分类id 拿到该分类下面的所有菜品信息
     */
    @GetMapping("/list")
    public RespBean list(Long categoryId) {
        log.info("获取的分类的id为: {}", categoryId);
        List<DishDto> dishList= dishService.getDishByCategory(categoryId);
        return dishList!=null?RespBean.ok("获取分类下的菜品信息成功",dishList):RespBean.error("获取分类下的菜品信息成功");
    }
}
